package yxy.game.pm2.command;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.message.MainMessage;
import yxy.game.pm2.module.gacha.GachaHeroHandle;
import yxy.game.pm2.module.gacha.GachaHeroModule;

/** 综合 新手引导抽将 */
@MessageType(proto = Command.GuideGacha, classtype = MainMessage.GuideGachaRequest.class)
public class CMD_GuideGacha extends Command<MainMessage.GuideGachaRequest> {

	@Override
	public void execute(MessageSource messageSource, MainMessage.GuideGachaRequest request) {
		String playerCode = messageSource.getTag();
		int type = request.getType();

		MainMessage.GuideGachaResponse.Builder response = MainMessage.GuideGachaResponse.newBuilder();

		// 封装消息
		GachaHeroModule module = GachaHeroModule.get(playerCode);
		try {
			if (module.tryLock()) {
				Result result = new GachaHeroHandle().summonGuide(module, type, response);

				send(messageSource, response, result);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			module.unlock();
		}

	}

	private void send(MessageSource messageSource, MainMessage.GuideGachaResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}